﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using DapperExtensions.SqlServerExt;

namespace dapper.Repository.Base
{
    /// <summary>
    /// 封装的dapper仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="T1"></typeparam>
    public class BaseReponsitory<T, T1> where T : BaseEntity<T1>
    {
        //根据Id查询实体
        public T GetById(T1 id, IDbConnection con, IDbTransaction transaction = null, string returnFields = null)
        {
            var entity = con.GetById<T>(id, returnFields, transaction);
            return entity;
        }

        //插入数据
        public virtual void Insert(T entity, IDbConnection con, IDbTransaction transaction = null)
        {
            con.Insert<T>(entity, transaction);
        }

        /// <summary>
        /// 插入多条数据
        /// </summary>
        /// <param name="entities">Entities</param>
        public virtual void Insert(IEnumerable<T> entities, IDbConnection con, IDbTransaction transaction = null)
        {

        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity">Entity</param>
        public virtual void Update(T entity, IDbConnection con, IDbTransaction transaction = null ,string updateFields = null)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
            con.UpdateById(entity, updateFields, transaction);
        }

        /// <summary>
        /// 修改多条数据
        /// </summary>
        /// <param name="entities">Entities</param>
        public virtual void Update(IEnumerable<T> entities, IDbConnection con, IDbTransaction transaction = null)
        {

        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity">Entity</param>
        public virtual void Delete(T entity, IDbConnection con, IDbTransaction transaction = null)
        {
             con.DeleteById<T>(entity.Id, transaction);
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="entities">Entities</param>
        public virtual void Delete(IEnumerable<T> entities, IDbConnection con, IDbTransaction transaction = null)
        {

        }

        /// <summary>
        /// 分页查询单表数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetByPageIndex(int pageIndex, int pageSize, IDbConnection con,IDbTransaction transaction = null, string returnFields = null, string where = null, object param = null, string orderBy = null)
        {
                return con.GetByPageIndex<T>(pageIndex, pageSize, returnFields, where, param, orderBy, transaction);
        }

        /// <summary>
        /// 根据sql语句获取表格
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public virtual DataTable GetDataTable(string sql, IDbConnection con, IDbTransaction transaction = null, object param = null)
        {
            return con.GetDataTable(sql, param, transaction);
        }

        /// <summary>
        /// 返回自定类型DTO
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public virtual IEnumerable<T2> GetDynimic<T2>(string sql, IDbConnection con, IDbTransaction transaction = null, object param = null)
        {
            return con.Query<T2>(sql, param, transaction);
        }


    }
}
